
package ch.hearc.cryptologie.cryptographie.decalage.rot13.obj;

public class Rot13
	{
	
	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/
	private int m_level;
	
	/*------------------------------*\
	|*			  Static			*|
	\*------------------------------*/
	public static final int ROT13 = 13;
	public static final int MIN_ASCII = 97;
	public static final int MAX_ASCII = 122;
	public static final int NB_CHAR = 26;
	
	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public Rot13(int level)
		{
		m_level = level;
		}
	
	public Rot13()
		{
		this(ROT13);
		}
	
	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/
	public String crypt(String msg)
		{
		return this.rotAlgo(msg);
		}
	
	public String decrypt(String cryptedMsg)
		{
		return this.rotAlgo(cryptedMsg);
		}
	
	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	private String rotAlgo(String str)
		{
		char[] tabMsg = str.toLowerCase().toCharArray();
		
		for(int i = 0; i < tabMsg.length; i++)
			{
			int c = tabMsg[i] + m_level;
			if (c > MAX_ASCII)
				{
				c = ((c - MIN_ASCII) % NB_CHAR) + MIN_ASCII;
				}
			
			tabMsg[i] = (char)c;
			}
		
		return String.valueOf(tabMsg);
		}
	}
